package com.example.demo.controller;

import com.example.demo.dao.FileDao;
import com.example.demo.entity.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("file")
@RestController
public class FileController {

    @Autowired
    private FileDao fileDao;

    @RequestMapping("/download")
    public Object download(File file, HttpSession session) {
        if (session.getAttribute("USER_ID") == null) {
            return null;
        }
        Integer userId = (Integer)session.getAttribute("USER_ID");
        file.setUser_id(userId);
        fileDao.add(file);
        return "OK";
    }

    @RequestMapping("/list")
    public List<File> list(HttpSession session) {
        if (session.getAttribute("USER_ID") == null) {
            return null;
        }
        Integer userId = (Integer)session.getAttribute("USER_ID");
        return fileDao.listFile(userId);

    }

}
